import {getIcons} from '../../utils';

export const initData = (sourceData, associateType, modelInfo) => {
  let moduleIcons = localStorage.getItem('moduleIcons');
  if(moduleIcons) moduleIcons = JSON.parse(moduleIcons);
  let data = {nodes: [], edges: [...sourceData.edges]};
  let nodeIds = new Map();
  sourceData.nodes?.unshift({// 添加中心节点，没关联关系时，只显示中心节点
    srcModuleId: modelInfo.cmModuleId,
    srcModuleCode: modelInfo.moduleCode,
    srcModuleName: modelInfo.moduleName,
    destModuleId: modelInfo.cmModuleId,
    destModuleCode: modelInfo.moduleCode,
    destModuleName: modelInfo.moduleName,
    cmModuleId: modelInfo.cmModuleId,
    isRoot: true,
    collapsed: false,
    isAdd: modelInfo.isAdd || false,
    closeActive: true,
    type: 'center-node',
    color: '#40a9ff',
    size: 30, //圆的大小
    style: {
      // offset: 11,
      fill: '#4E84E9',
      cursor: 'pointer',
    },
    labelCfg: {
      position: 'bottom',
      offset: 10,
      style: {
          position: 'bottom',
          offset: 10,
          fill: '#666',
          fontSize: 12,
      },
    },
  })
  sourceData.nodes?.map(item=> {
    const {srcModuleId, srcModuleName, srcModuleCode, destModuleId, destModuleName, destModuleCode } = item;
    if(srcModuleId && !nodeIds.get(srcModuleId)) {
      nodeIds.set(srcModuleId, item);
      data.nodes.push({
        ...item,
        id: srcModuleId,
        label: srcModuleName,
        moduleId: srcModuleId,
        moduleName: srcModuleName, 
        moduleCode: srcModuleCode,
        icon: getIcons(moduleIcons[srcModuleId]),
        cmModuleId: srcModuleId,
      })
    }

    if(destModuleId && !nodeIds.get(destModuleId)) {
      nodeIds.set(destModuleId, item);
      data.nodes.push({
        ...item,
        id: destModuleId,
        label: destModuleName,
        moduleId: destModuleId,
        moduleName: destModuleName, 
        moduleCode: destModuleCode,
        icon: getIcons(moduleIcons[srcModuleId]),
        cmModuleId: destModuleId,
      })
    }
  })

  data.edges?.forEach((edge, index) => {
    Object.assign(edge, {
        source: edge.srcModuleId,
        target: edge.destModuleId,
        label: associateType?.[edge.associateType]??edge.associateType,
    })

    if(edge.srcModuleId == modelInfo.cmModuleId || edge.destModuleId == modelInfo.cmModuleId ) { //中心节点
      edge.type = 'circle-running'
    }
  })

  return data
}
